require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userCategory=JSON.parse(sessionStorage.getItem("userCategory"));
var vm = new Vue({
    el:"#postDetails",
    data:{
        isShare:false,
        loadFlag:false,
        postId:"",//帖子id
        loginUid:"",//用户id
        pageNumber:1,
        postInfo:{},
        isPraise:"",  //是否点赞  1点赞0没点赞
        praiseCount:"",
        commentList:{},//评论
        commentList1:[],
        commentList2:[],
        commentText:"",
        topBg:false
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.postId=this.searchObject&&this.searchObject.postId;
        this.loginUid=this.searchObject&&this.searchObject.uid;
        this.getPost();
        this.getInfoPostCommentList();
    },
    methods:{
        //后退
        getBack: function () {
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //获取帖子详情
        getPost:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                postId:that.postId
            };
            this.ajax({url:"/friend/getPost",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.postInfo=b.json.data;
                    that.isPraise=that.postInfo.isPraise;
                    that.praiseCount=that.postInfo.praiseCount;
                    //console.log(that.categoryList)
                }else{
                    vm.$refs.prompt.toast("获取帖子详情失败！")
                }
            });

        },
        //获取帖子评论列表
        getInfoPostCommentList:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                postId:that.postId,
                pageNumber:that.pageNumber
            };
            //console.log(data)
            this.ajax({url:"/friend/getInfoPostCommentList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.commentList=b.json.data;
                    that.commentList1=that.commentList.list;
                    if(that.commentList.pageNumber>1){
                        if(that.commentList.list.length>0) {
                            that.commentList2 = that.commentList2.concat(b.json.data.list);
                        }
                    }else{
                        that.commentList2= that.commentList1;
                    }
                    vm.loadFlag=false;
                    console.log(that.commentList2)
                }else{
                    that.commentList={};
                    that.commentList1=[];
                    that.commentList2=[];
                    vm.$refs.prompt.toast("获取评论列表失败！");
                }
            });
        },
        //点赞
        getPraise:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                postId:that.postId
            };
            if(that.isPraise==0){
                data.state=1;
            }else{
                data.state=0;
            }
            console.log(data);
            this.ajax({url:"/friend/praisePost",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    // console.log(b.json.data)
                    // that.praiseCount=b.json.data.praiseCount;
                    // if(that.isPraise==0){
                    //     that.isPraise=1;
                    // }else{
                    //     that.isPraise=0;
                    // }
                    // console.log(that.isPraise)
                    that.getPost();
                }else{
                    vm.$refs.prompt.toast("点赞失败！")
                }
            });
        },
        //删除帖子
        deletePost:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                postId:that.postId
            };
            if(that.postInfo.isCircleUser==1&&that.postInfo.isPostUser==1||that.postInfo.isCircleUser==1&&that.postInfo.isPostUser==0){   //1系统 2圈主 3自己
                data.delType=2
            }else if(that.postInfo.isCircleUser==0&&that.postInfo.isPostUser==1){
                data.delType=3
            }
            console.log(data);
            this.ajax({url:"/friend/deletePost",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.getBack();
                }else{
                    vm.$refs.prompt.toast("删除帖子失败！");
                }
            });
        },
        //设置精选
        setSelected:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                postId:that.postId
            };
            if(that.postInfo.isSelected==1){    // 1设为精选 0取消精选
                data.state=0
            }else if(that.postInfo.isSelected==0){
                data.state=1
            }
            console.log(data);
            this.ajax({url:"/friend/setSelected",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    if(that.postInfo.isSelected==1){    // 1设为精选 0取消精选
                        that.postInfo.isSelected=0
                    }else if(that.postInfo.isSelected==0){
                        that.postInfo.isSelected=1
                    }
                }else{
                    vm.$refs.prompt.toast("设置失败！");
                }
            });
        },
        //设置精选
        setTop:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                postId:that.postId
            };
            if(that.postInfo.isTop==1){    // 1设为精选 0取消精选
                data.state=0
            }else if(that.postInfo.isTop==0){
                data.state=1
            }
            console.log(data);
            this.ajax({url:"/friend/setSelected",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    if(that.postInfo.isTop==1){    // 1设为精选 0取消精选
                        that.postInfo.isTop=0
                    }else if(that.postInfo.isTop==0){
                        that.postInfo.isTop=1
                    }
                }else{
                    vm.$refs.prompt.toast("设置失败！");
                }
            });
        },
        //设置公告
        setNotice:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                postId:that.postId
            };
            if(that.postInfo.isNotice==1){    // 1设为精选 0取消精选
                data.state=0
            }else if(that.postInfo.isNotice==0){
                data.state=1
            }
            console.log(data);
            this.ajax({url:"/friend/setNotice",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    if(that.postInfo.isNotice==1){    // 1设为精选 0取消精选
                        that.postInfo.isNotice=0
                    }else if(that.postInfo.isNotice==0){
                        that.postInfo.isNotice=1
                    }
                }else{
                    vm.$refs.prompt.toast("设置失败！");
                }
            });
        },
        //圈子资料
        getNext:function(){
            location.href="/friend/circleInfo?circleId="+this.postInfo.circleId+"&uid="+this.loginUid;

        },
        //评论点赞
        praiseMediaAdvComment:function(state,commentId){
            var that=this;
            console.log(state+"----"+commentId)
            var data={
                loginUid:that.loginUid,
                commentId :commentId
            };
            if(state==0){
                data.state=1;
            }else{
                data.state=0;
            }
            console.log(data)
            this.ajax({url:"/friend/praisePostComment",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.commentList2.forEach(function(val){
                        if(val.commentId==commentId){
                            val.praiseState=data.state;
                            val.praiseCount=b.json.data.praiseCount;
                        }
                    });
                    console.log(b.json.data)
                }else{
                    vm.$refs.prompt.toast("点赞失败！");
                }
            });
        },
        //发表评论
        comment:function(){
            var that=this;
            if(!that.commentText){
                vm.$refs.prompt.toast("请输入评论内容！");
                return false;
            }
            var data={
                loginUid:that.loginUid,
                postId:that.postId,
                content:that.commentText
            };
            console.log(data);
            this.ajax({url:"/friend/postComment",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.commentText="";
                    that.getInfoPostCommentList();
                    //console.log(b.json.data)
                }else{
                    vm.$refs.prompt.toast("评论失败！");
                }
            });
        },
        getCommentInfo:function(commentId){
            location.href="/see/commentInfo?type=2&id="+this.postId+"&loginUid="+this.loginUid+"&commentId="+commentId;
        },
        getShare:function(){
            this.isShare=!this.isShare;
        },
        getChoice:function(num){
            if(num==1){
                location.href="/friend/choiceList?number=0&postId="+this.postId;
            }else{
                location.href="/friend/forward?number=0&postId="+this.postId;
            }

        }
    },
    watch:{
        txt:function(){
            this.num=this.txt.length;
            //console.log(this.num);
        }
    }
});
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    if(getDocumentTop()>0){
        vm.topBg=true;
    }else{
        vm.topBg=false;
    }
    var loadFlag = vm.commentList.lastPage;
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.pageNumber++;
            vm.getCommentsByPostId();
        }else{
            vm.$refs.prompt.toast("暂无更多回复")
        }
    }
};